/*
silverPDF is sponsored by Aleyant Systems (http://www.aleyant.com)

silverPDF is based on PdfSharp (http://www.pdfsharp.net) and iTextSharp (http://itextsharp.sourceforge.net)

Developers: Ai_boy (aka Oleksii Okhrymenko)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above information and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR SPONSORS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System.IO;

namespace BusinessCard
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        MemoryStream _bitmapImage;

        private void imageButton1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();
            d.Filter = "Image file|*.jpg;*.png";

            if (d.ShowDialog() == true)
            {
                imageSelect.Text = "Select image (" + d.File.Name + ")";

                var m = new MemoryStream();
                var obj = d.File.OpenRead();

                var bytes = new byte[obj.Length];
                obj.Read(bytes, 0, bytes.Length);
                obj.Seek(0, SeekOrigin.Begin);

                _bitmapImage = new MemoryStream(bytes);
            }
            
        }

        private void saveButton1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "PDF file format|*.pdf";

            if (_bitmapImage == null)
            {
                MessageBox.Show("Select an image!");
                return;
            }

            if (d.ShowDialog() == true)
            {
                var document = new PdfDocument();
                document.Info.Title = "silverPDF Buisness Card Sample";
                document.Info.Author = "Ai_boy";
                document.Info.Subject = "Created for showing power of silverPDF library";
                document.Info.Keywords = "PDFsharp, silverPDF, Silverlight";

                var page = document.AddPage();
                page.Width = new XUnit(2, XGraphicsUnit.Inch);
                page.Height = new XUnit(3.5, XGraphicsUnit.Inch);
                page.Orientation = PdfSharp.PageOrientation.Landscape;

                var g = XGraphics.FromPdfPage(page);
                var font = new XFont("Arial", 12);
                var image = XImage.FromStream(_bitmapImage);

                g.DrawRectangle(XBrushes.Black, new XRect(15, 15, 210, 110));
                g.DrawRoundedRectangle(XBrushes.SeaGreen, new XRect(20, 20, 200, 100), new XSize(10, 10));
                g.DrawImage(image, new XRect(30, 30, 50, 50));
                g.DrawString(nameTextBox.Text, font, XBrushes.White, new XRect(90, 40, 130, 0), XStringFormats.TopLeft); 

                document.Save(d.OpenFile());
            }
        }
    }
}
